package customerEnter.Frame.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import customerEnter.Frame.Utils.DateUtils;
import customerEnter.Frame.filterDataTransmission.IpRecord;
import customerEnter.tool.WhetherToEmpty;

/**
 * ip訪問頻率限制
 * 
 * 类名称：ipFilter 类描述： 创建人：dy 创建时间：2016-4-30 下午3:14:08
 * 
 */
public class ipFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException,
			ServletException {

		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		doRequestIn(request, response);
		String UserIp = getIpAddr(request);
		String uri = request.getRequestURI();
		String path = uri;
		if (uri.length() >= 12) {
			path = uri.substring(12);
		}
		// 第一次訪問：
		if (WhetherToEmpty.StringWhetherToEmpty(IpRecord.getIpRecord().getIpRecordTime(UserIp))) {
			initChain(UserIp, request, response, chain, path);
		} else if (!WhetherToEmpty.StringWhetherToEmpty(UserIp) && path.contains("jsp")) {
			String url = "";
			try {
				url = ((FilterConfig) req).getServletContext().getAttribute("Url").toString();
			} catch (Exception e) {
				url = "";
			}
			try {
				if ((DateUtils.dateToUnixTimestamp() - IpRecord.getIpRecord().getIpRecord(UserIp)) <= 800
						&& path.equals(url)) {
					IpRecord.getIpRecord().setIpRecord(UserIp, DateUtils.dateToUnixTimestamp(), DateUtils.getNowTime());
					req.getRequestDispatcher("/fram/slipe.jsp").forward(req, resp);
				} else {
					initChain(UserIp, request, response, chain, path);
				}
			} catch (Exception e) {
				System.err.println("cpu不夠了");
				e.printStackTrace();
			}
		} else {
			initChain(UserIp, request, response, chain, path);
		}

	}

	private void doRequestIn(HttpServletRequest request, HttpServletResponse response) {

	}

	public void initChain(String UserIp, HttpServletRequest req, ServletResponse resp, FilterChain chain, String Url) {
		IpRecord.getIpRecord().setIpRecord(UserIp, DateUtils.dateToUnixTimestamp(), DateUtils.getNowTime());
		if (Url.contains(".jsp")) {
			((FilterConfig) req).getServletContext().setAttribute("Url", Url);
		}
		try {
			chain.doFilter(req, resp);
		} catch (Exception e) {

		}
	}

	/**
	 * 獲取用戶真實ip
	 */
	public String getIpAddr(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		return ip;
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}
}
